function formatNumber (n) {
    const str = n.toString()
    return str[1] ? str : `0${str}`
}
export function formatTime (date) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    const hour = date.getHours()
    const minute = date.getMinutes()
    // const second = date.getSeconds()

    const t1 = [year, month, day].map(formatNumber).join('-')
    const t2 = [hour, minute].map(formatNumber).join(':')

    return `${t1} ${t2} `
}
export function formatTimeIncludeText (date, fmt) {
    //将时间戳格式化为xxxx年xx月xx日
    date = new Date(parseInt(date))
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    return formatNumber(year)+'年'+ formatNumber(month)+ '月'+ formatNumber(day) +'日'
}
export function getUserIP(onNewIP) { //  onNewIp - your listener function for new IPs
    //compatibility for firefox and chrome
    var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
    var pc = new myPeerConnection({
            iceServers: []
        }),
        noop = function() {},
        localIPs = {},
        ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
        key;

    function iterateIP(ip) {
        if (!localIPs[ip]) onNewIP(ip);
        localIPs[ip] = true;
    }

    //create a bogus data channel
    pc.createDataChannel("");

    // create offer and set local description
    pc.createOffer().then(function(sdp) {
        sdp.sdp.split('\n').forEach(function(line) {
            if (line.indexOf('candidate') < 0) return;
            line.match(ipRegex).forEach(iterateIP);
        });

        pc.setLocalDescription(sdp, noop, noop);
    }).catch(function(reason) {
        // An error occurred, so handle the failure to connect
    });

    //sten for candidate events
    pc.onicecandidate = function(ice) {
        if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
        ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
    };
}

export function dec2hex (dec, len) {
    var hex = "";
    while( dec ) {
        var last = dec & 15;
        hex = String.fromCharCode(((last>9)?55:48)+last) + hex;
        dec >>= 4;
    }
    if(len) {
        while(hex.length < len) hex = '0' + hex;
    }
    return hex;
};
export function toFixedBit(num, totalBit, isFront, fixedChar, alwaysCut) {
    if (totalBit === void 0) { totalBit = 10; }
    if (isFront === void 0) { isFront = true; }
    if (fixedChar === void 0) { fixedChar = "0"; }
    if (alwaysCut === void 0) { alwaysCut = false; }
    var nn = num.toString();
    if (!alwaysCut && nn.length >= totalBit) {
        return nn;
    }
    var rtn = "";
    for (var i = 0; i < totalBit; i++) {
        rtn += fixedChar;
    }
    if (isFront) {
        rtn += nn;
        rtn = rtn.substr(rtn.length - totalBit);
    }
    else {
        rtn = nn + rtn;
        rtn = rtn.substr(0, totalBit);
    }
    return rtn;
};
export function strToHexCharCode(str,len) {
    var val="";
    var strCode =''
    for(var i = 0; i < str.length; i++){
        if(val == "")
            val = str.charCodeAt(i).toString(16);
        else
            val +=  str.charCodeAt(i).toString(16);
    }
    return val
}

export function  clearInfo() {

}
export function  getNewDate(str){
    if(!str){
        return 0;
    }
    var arr=str.split(" ");
    var d=arr[0].split("-");
    var t=arr[1].split(":");
    var date = new Date();
    date.setUTCFullYear(d[0], d[1] - 1, d[2]);
    date.setUTCHours(t[0], t[1], 0);
    return date;
}
export function  formatDateTime(timeStamp) {
    if(!timeStamp){
        return ''
    }
    else{
        var date = new Date();
        date.setTime(timeStamp * 1000);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        return y + '-' + m + '-' + d
    }
};
export function  formatMonth(timeStamp) {
    if(!timeStamp){
        return ''
    }
    else{
        var date = new Date();
        date.setTime(timeStamp);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        return y + '年' + m + '月'
    }
};


export function  formatgetTime(timeStamp) {
    var date =new Date(timeStamp);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    return y + '-' + m + '-' + d
};
export function  formatDateYear(timeStamp) {
    var date = new Date();
    date.setTime(timeStamp * 1000);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;

    return y
};
//获取当前时间所在周的周一
export function GetMonday(dd) {
    var week = dd.getDay(); //获取时间的星期数
    var minus = week ? week - 1 : 6;
    dd.setDate(dd.getDate() - minus); //获取minus天前的日期
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1; //获取月份
    m = m < 10 ? ('0' + m) : m;
    var d = dd.getDate();
    d = d < 10 ? ('0' + d) : d;
    return y + "-" + m + "-" + d;
}
//获取cookie、
export function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return (arr[2]);
    else
        return null;
}

//设置cookie,增加到vue实例方便全局调用
export function setCookie (c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setTime(exdate.getTime() + expiredays*1000*60);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
};

//删除cookie
export function delCookie (name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null)
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
};